package com.gxa.ehome.controller;

import com.gxa.ehome.mapper.SysUserMapper;
import com.gxa.ehome.pojo.SysUser;
import com.gxa.ehome.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/index")
public class indexController {
    //响应前端请求,没有任何页面可以展示
    //在请球hello的时候,去获取web程序中的对象
    //HttpServlet/HttpServletRequest/HttpServletResponse
    //HttpServletContext/ServletConfig/Cookie/Session
    @RequestMapping("/hello")
    public void hello(HttpServletRequest request) {
        System.out.println("request"+request);
        ServletContext servletContext = request.getServletContext();
        System.out.println("servletContext :"+servletContext.getRealPath("/"));
        //通过上下文对象,获取目标资源的本地路径
        //表示当前运行java程序所在的本地目录
        System.out.println("有前端发起了对/index/hello 的请求");

    }

    //访问jsp页面 springmvc的返回值就是在转发.
    //将数据代入到jsp中.request,setAttribute()
    //ModelMao:存放数据的数据模型
    //ModelAndView 包含数据和页面信息整合对象.
    //@ModelAttribute:通过name/value指定的数据名称,可以吧
    //方法中定义好得对象传递
    @RequestMapping("/index")
    // @ModelAttribute(name = "username",value ="admin")
    public String index(ModelMap modelMap) {
    modelMap.addAttribute("username","admin");
//        return "/WEB-INF/jsp/index.jsp";
        //当配置了前后最之后
        return "index";
    }
    //利用produces(设定响应方式,来解决直接使用@ResponseBody给前端传递中文字符串)
    //利用produces(设定响应方式 application/json:charset=utf-8)
    //几乎没有应用场景
    @RequestMapping(value = "/string",produces = ("application/json;charset=utf-8"))
    @ResponseBody
    public String getString(){
        return "你好,来啦";
    }
    //当我们要给前端返回对象,实际上是对象做json格式的转换处理
    //使用fastjson等json工具做序列化处理:{jobNumber:"GL001",name:"zhangsan"}


    //否则会报出:No converter found
    //解决方法 fastjson(不推荐) /jackson(另一种json格式处理,推荐)
    //只需要导入jackson的jar包
    @RequestMapping("/sysUser")
    @ResponseBody
    public SysUser getSysUser(){
        SysUser sysUser=new SysUser();
        sysUser.setName("张敏");
        sysUser.setJobNumber("GLOO1");
        return sysUser;
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<SysUser> getList(@RequestHeader("token") String token){
        System.out.println("token:"+token);
        SysUser sysUser=new SysUser();
        sysUser.setName("张敏");
        sysUser.setJobNumber("GLOO1");
        SysUser sysUser2=new SysUser();
        sysUser2.setName("张无忌");
        sysUser2.setJobNumber("GLOO2");
        List<SysUser> sysUsers=new ArrayList <>();
        sysUsers.add(sysUser2);
        sysUsers.add(sysUser);
        return sysUsers;
    }
        @Resource
   SysUserMapper sysUserMapper=null;
    @RequestMapping("/map")
    @ResponseBody
    public Map<String,Object> getMap(){
        HashMap<String,Object> map=new HashMap <>();
        map.put("我好帅",sysUserMapper.findAll());
       map.put("你好丑",5);

        return map;
    }
}
